package com.hzhr.iot.device.sdk.service;

import com.hzhr.iot.core.constant.Common_ErrorsEnum;
import com.hzhr.iot.core.dto.ResponseExceptionDto;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrHttpUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.device.sdk.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.sdk.model.pressureMonitor.PressureMonitorDeleteModel;
import com.hzhr.iot.device.sdk.model.pressureMonitor.PressureMonitorDeleteResponseModel;
import com.hzhr.iot.device.sdk.model.pressureMonitor.PressureMonitorPostModel;
import com.hzhr.iot.device.sdk.model.pressureMonitor.PressureMonitorPostResponseModel;
import com.hzhr.iot.device.sdk.model.pressureMonitor.PressureMonitorPutModel;
import com.hzhr.iot.device.sdk.model.pressureMonitor.PressureMonitorPutResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Service
public class Device_PressureMonitorService {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 实施档案-修改流量计台账 <br/><br/>
     * 简介 : 修改压力监测仪采集台账；关联对应的物联台账及燃气用户 <br/>
     * 步骤 : <br/>
     * 1. 系统权限：根据压力监测仪编号查询“压力监测仪采集台账_1”，如果“压力监测仪采集台账_1”不存在，抛出异常“压力监测仪采集台账不存在”; (√) <br/>
     * 2. 系统权限：如果压力监测仪代码与“压力监测仪采集台账_1”的代码不同，则根据压力监测仪代码查询“压力监测仪物联台账_1”，如果“压力监测仪物联台账_1”不存在，抛出异常“新的压力检测物联台账不存在”，然后根据压力监测仪代码和燃气公司编号查询“压力监测仪采集台账_2”，如果“压力监测仪采集台账_2”存在，则抛出异常“新的压力监测仪代码已存在”; (√) <br/>
     * 3. 系统权限：如果燃气用户代码和“压力监测仪采集台账_1”的燃气用户代码不同，则根据燃气用户代码和部门编号查询“燃气用户_1”，如果“燃气用户_1”不存在，抛出异常“燃气用户不存在”；(√) <br/>
     * 4. 系统权限：更新“压力监测仪采集台账_1”的信息，如果压力监测仪代码与“压力监测仪采集台账_1”的代码不同，关联“压力监测仪采集台账_1”和“压力监测仪物联台账_1”，如果燃气用户代码和“压力监测仪采集台账_1”的燃气用户代码不同，关联“压力监测仪采集台账_1”和“燃气用户_1”; (√) <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see PressureMonitorPutModel body <br/>
     * @see PressureMonitorPutResponseModel result <br/>
     */ 
    public PressureMonitorPutResponseModel put(Map<String, String> header, Map<String, Object> query, PressureMonitorPutModel body) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(body, headers);

        Map<String, Object> uriVariables = new HashMap<>();

        try {
            ResponseEntity<PressureMonitorPutResponseModel> result = restTemplate.exchange("http://device-service/pressure-monitors?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.PUT, httpEntity, PressureMonitorPutResponseModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("PRESSUREMONITORSERVICE_PUT_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("PRESSUREMONITORSERVICE_PUT_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
    /**
     * 实施档案-删除压力检测仪台账 <br/><br/>
     * 简介 : 删除压力监测仪采集台账 <br/>
     * 步骤 : <br/>
     * 1. 系统权限：根据压力监测仪编号查询“压力监测仪采集台账_1”，如“压力监测仪采集台账_1”不存在，抛出异常“压力监测仪采集台账不存在”; (√) <br/>
     * 2. 系统权限：删除“压力监测仪采集台账_1”; (√) <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see PressureMonitorDeleteModel body <br/>
     * @see PressureMonitorDeleteResponseModel result <br/>
     */ 
    public PressureMonitorDeleteResponseModel delete(Map<String, String> header, Map<String, Object> query, PressureMonitorDeleteModel body) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(body, headers);

        Map<String, Object> uriVariables = new HashMap<>();

        try {
            ResponseEntity<PressureMonitorDeleteResponseModel> result = restTemplate.exchange("http://device-service/pressure-monitors?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.DELETE, httpEntity, PressureMonitorDeleteResponseModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("PRESSUREMONITORSERVICE_DELETE_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("PRESSUREMONITORSERVICE_DELETE_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
    /**
     * 实施档案-创建流量计台账 <br/><br/>
     * 简介 : 创建压力监测仪采集台账；关联对应的物联台账及燃气用户 <br/>
     * 步骤 : <br/>
     * 1. 系统权限：根据燃气用户代码和部门编号查询“燃气用户_1”，如果“燃气用户_1”不存在，抛出异常“燃气用户不存在”；(√) <br/>
     * 2. 系统权限：根据压力监测仪代码查询“压力监测仪物联台账_1”，如果“压力监测仪物联台账_1”不存在，抛出异常“压力监测仪物联台账不存在”; (√) <br/>
     * 3. 系统权限：根据压力监测仪代码和燃气公司编号查询“压力监测仪采集台账_1”，如果“压力监测仪采集台账_1”存在，抛出异常“压力监测仪代码已存在”; (√) <br/>
     * 4. 系统权限：在燃气公司和部门下创建“压力监测仪采集台账_2”，并关联“压力监测仪物联台账_1”和“燃气用户_1”; (√) <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see PressureMonitorPostModel body <br/>
     * @see PressureMonitorPostResponseModel result <br/>
     */ 
    public PressureMonitorPostResponseModel post(Map<String, String> header, Map<String, Object> query, PressureMonitorPostModel body) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(body, headers);

        Map<String, Object> uriVariables = new HashMap<>();

        try {
            ResponseEntity<PressureMonitorPostResponseModel> result = restTemplate.exchange("http://device-service/pressure-monitors?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.POST, httpEntity, PressureMonitorPostResponseModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("PRESSUREMONITORSERVICE_POST_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("PRESSUREMONITORSERVICE_POST_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
}

